package com.fivetalks.client;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RoomDialogActivity extends Activity 
implements OnClickListener,OnCheckedChangeListener, Runnable
 {
	EditText etRoomTitle;
	RadioGroup rgMaxPerson;
	int MaxPerson;
	ImageButton btSubmit;
	ImageButton btCancel;
	Thread m_thread;
	
	boolean isThreadEnd = false;
	long roomNumber,roomNo;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_room_dialog);
		
		
		btSubmit=(ImageButton)findViewById(R.id.btSubmit);
		btSubmit.setOnClickListener(this);
		
		rgMaxPerson = (RadioGroup)findViewById(R.id.rgMaxPerson);
        rgMaxPerson.setOnCheckedChangeListener(this);
        MaxPerson = 1;
        
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btSubmit:
			start();
			
			
			break;
		}
	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}



	public void run() {
		
		SocketClient sc = SocketClient.getInstance(null,RoomDialogActivity.this,null,null,null,null,null,null);
		
		// room create.
		EditText etRoomTitle=(EditText)findViewById(R.id.etRoomTitle);
		
		try {
			sc.getDos().writeUTF("createRoom");
			sc.getDos().writeUTF(etRoomTitle.getText().toString());
			sc.getDos().writeUTF(MaxPerson+"");
			sc.getDos().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		while(!isThreadEnd){
			
		}
		Intent intent=new Intent(this,PreChatActivity.class);
		intent.putExtra("roomNumber", roomNo);
		Log.d("RoomDialog","Before Intent: "+roomNo);
		startActivity(intent);
	}
			

	@Override
    public void onCheckedChanged(RadioGroup group,int checkedId)
    {
    	MaxPerson=Integer.parseInt(((RadioButton)findViewById(checkedId)).getText().toString());
    }

 }
